#!/usr/bin/env ruby

=begin
    For more information
    http://rspec.rubyforge.org/documentation/mocks/mocks.html
=end

require 'postgres'

class Database
    def initialize(host, port, database, user, pass)
        @host, @port, @database, @user, @pass = host, port, database, user, pass
    end
    
    def connect()
        begin
            @conn = PGconn.new(@host, @port, nil, nil , @database, @user, @pass)
        rescue PGError => err
            p "Exception:", err
        end
    end
    
    def run_query(sql)
        res = @conn.exec(sql)
        res
    end
end

class Customer
    def initialize(name, db)
        @name, @db = name, db
        @db.connect
    end
    
    def say_hello()
        q = %Q(select name from \"public\".\"customers\" where name = '#{@name}')
        res = @db.run_query(q)
    end
end

#@db = Database.new("localhost", 5432, "smsclient", "nauman", "password")
#c = Customer.new("John Doe", @db)
# print c.say_hello()

describe "Customer", 'When new instance of Customer is created' do
    
    before do
        @mock_db = mock('Database')
        @mock_db.should_receive(:connect)
    end
    
    it 'should initialize and connect to the database' do
        @customer = Customer.new("John Doe", @mock_db)
    end
end

describe Customer, "When the customer is greeted" do
    
    before do
        @mock_db = mock('Database')
        @mock_db.should_receive(:connect)
        @mock_db.should_receive(:run_query)
    end
    
    it 'should say hello and execute the query' do
        @customer = Customer.new("John Doe", @mock_db)
        @customer.say_hello()
    end
    
end
